/*
 * @Description: 
 * @Author: yeeChen
 * @Date: 2023-01-06 16:39:07
 * @LastEditTime: 2023-01-06 16:54:13
 * @LastEditors: yeeChen
 */
// 导入express
const express = require('express')
const session = require('express-session')
const app = express()

app.use(session({
    secret: 'itheima',
    resave: false,
    saveUninitialized:true,
}))
app.use(express.urlencoded({ extended: false }))

app.post('/api/post', (req, res) => {
    if (req.body.username !== 'admin' || req.body.password !== '000000') {
        return res.send({status:1,msg:'登录失效'})
    }

req.session.user = req.body // 将用户信息储存到session
req.session.isLogin = true // 将用户登录状态存储到session
req.send({status:0,msg:'登录成功'})
})

app.get('/api/username', (req, res) => {
    if(!req.session.isLogin){
    return res.send({status:0,msg:'请先登录'})
    }
    res.send({status:0,msg:'success',username:req.session.user.username})
})

app.post('/api/logout', (req, res) => {
    req.session.destroy()
    res.send({
        status: 1,
        msg:'退出登录成功'
    })
})
